package com.cardone.platform.authority.dao;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.authority.dto.RolePermissionDto;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 角色与许可
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class RolePermissionJdbcDao implements RolePermissionDao {
	@Override
	public int deleteByIds(final RolePermissionDto deleteRolePermission) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deleteRolePermission.getAttrs())) {
			model.putAll(deleteRolePermission.getAttrs());
		}

		model.put(Attributes.ids.name(), deleteRolePermission.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public int deleteByRoleIds(final String roleIds) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put("roleIds", roleIds);

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByRoleIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final RolePermissionDto findRolePermission) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findRolePermission, Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findRolePermission, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findRolePermission.getAttrs())) {
			model.putAll(findRolePermission.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final RolePermissionDto findRolePermission) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findRolePermission);

		final Model model = ModelUtils.newModel(new ModelArgs(findRolePermission, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findRolePermission.getAttrs())) {
			model.putAll(findRolePermission.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final RolePermissionDto findListRolePermission) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListRolePermission).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListRolePermission.getAttrs())) {
			model.putAll(findListRolePermission.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public int insertByCode(final RolePermissionDto insertRolePermission) {
		if (StringUtils.isBlank(insertRolePermission.getId())) {
			insertRolePermission.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertRolePermission));

		if (MapUtils.isNotEmpty(insertRolePermission.getAttrs())) {
			model.putAll(insertRolePermission.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final RolePermissionDto paginationRolePermission) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationRolePermission).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationRolePermission.getAttrs())) {
			model.putAll(paginationRolePermission.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationRolePermission), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final RolePermissionDto readRolePermission) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readRolePermission, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readRolePermission.getAttrs())) {
			model.putAll(readRolePermission.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readRolePermission);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readRolePermission, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final RolePermissionDto saveRolePermission) {
		final P oldRolePermission = this.findByCodeOrId(mappedClass, saveRolePermission);

		if (oldRolePermission == null) {
			this.insertByCode(saveRolePermission);

			return this.findByCodeOrId(mappedClass, saveRolePermission);
		}

		final int updateCount = this.updateByCode(saveRolePermission, oldRolePermission);

		if (updateCount < 1) {
			return oldRolePermission;
		}

		return this.findByCodeOrId(mappedClass, saveRolePermission);
	}

	@Override
	public Map<String, Object> saveRoleIdAndPermissionIds(final String roleId, final String permissionIds) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put(Attributes.roleId.name(), roleId);
		model.put("permissionIds", permissionIds);

		final Map<String, Object> returnMap = Maps.newHashMap();

		final int deleteCount = ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByRoleIdAndNotPermissionIds.id(), model);

		returnMap.put("deleteCount", deleteCount);

		if (StringUtils.isBlank(permissionIds)) {
			return returnMap;
		}

		final List<Map<String, Object>> mapList = Lists.newArrayList();

		final List<String> permissionIdList = Lists.newArrayList(StringUtils.split(permissionIds, ","));

		for (final String permissionId : permissionIdList) {
			final Map<String, Object> map = Maps.newHashMap();

			map.put(Attributes.roleId.name(), roleId);
			map.put("permissionId", permissionId);

			mapList.add(map);
		}

		final int insertCount = ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByRoleIdAndPermissionId.id(), mapList);

		returnMap.put("insertCount", insertCount);

		return returnMap;
	}

	@Override
	public int updateByCode(final RolePermissionDto updateRolePermission) {
		final RolePermissionDto oldRolePermission = this.findByCodeOrId(RolePermissionDto.class, updateRolePermission);

		return this.updateByCode(updateRolePermission, oldRolePermission);
	}

	private <P> int updateByCode(final RolePermissionDto updateRolePermission, final P oldRolePermission) {
		if (oldRolePermission == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.typeId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.remark.name() }, updateRolePermission);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateRolePermission, oldRolePermission, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldRolePermission, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldRolePermission, updateRolePermission, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateRolePermission, updateProperties), new ModelArgs(updateRolePermission, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateRolePermission.getAttrs())) {
			model.putAll(updateRolePermission.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}
}